package cn.bluemobi.dylan.step.moder.http;

import android.text.TextUtils;


import cn.bluemobi.dylan.step.moder.http.exception.ApiException;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


public class RxUtil {

//    /**
//     * 统一线程处理
//     *
//     * @param <T>
//     * @return
//     */
//    public static <T> ObservableTransformer<T, T> rxSchedulerHelper() {    //compose简化线程
//        return new ObservableTransformer<T, T>() {
//            @Override
//            public ObservableSource<T> apply(Observable<T> observable) {
//                return observable.subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread());
//            }
//        };
//    }

    /**
     * 统一线程处理
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<T, T> rxSchedulerHelper() {    //compose简化线程
        return new FlowableTransformer<T, T>() {
            @Override
            public Flowable<T> apply(Flowable<T> observable) {
                return observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


    /**
     * 统一返回结果处理
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<HttpResponseBean<T>, T> handleResult() {   //compose判断结果
        return new FlowableTransformer<HttpResponseBean<T>, T>() {
            @Override
            public Flowable<T> apply(Flowable<HttpResponseBean<T>> httpResponseFlowable) {
                return httpResponseFlowable.flatMap(new Function<HttpResponseBean<T>, Flowable<T>>() {
                    @Override
                    public Flowable<T> apply(HttpResponseBean<T> httpResponseBean) {
                        if (httpResponseBean.getResult()==0) {
                            return createData(httpResponseBean.getData());
                        } else if(!TextUtils.isEmpty(httpResponseBean.getMsg())){
                            return Flowable.error(new ApiException(httpResponseBean.getMsg(),httpResponseBean.getResult()));
                        }else {
                            return Flowable.error(new ApiException("服务器返回error",httpResponseBean.getResult()));
                        }
                    }
                });
            }
        };
    }


    /**
     * 生成Flowable
     *
     * @param <T>
     * @return
     */
    public static <T> Flowable<T> createData(final T t) {
        return Flowable.create(new FlowableOnSubscribe<T>() {
            @Override
            public void subscribe(FlowableEmitter<T> emitter) throws Exception {

                if(t==null){
                    try {
                        emitter.onComplete();
                    } catch (Exception e) {
                        emitter.onError(e);
                    }
                }else {
                    try {
                        emitter.onNext(t);   //传输的对象 不能为null
                        emitter.onComplete();
                    } catch (Exception e) {
                        emitter.onError(e);
                    }
                }
            }
        }, BackpressureStrategy.BUFFER);
    }

}
